import {
  indexApi
} from '../../../api/index';
import {
  request
} from '../../../utils/request';
const app = getApp();
Page({
  data: {
    defaultData: {
      title: "我的商品", // 导航栏标题
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    productList: [],
    pageNum: 0,
    pageSize: 10,
    isEmpty: false,
    radio: '1',
    selectAddress: "",
    fromPage: null,
    serviceTypeList: [],
    serviceTypeId: null,
    isEmpty: false
  },


  onLoad(options) {
    if (options.fromPage) {
      this.setData({
        fromPage: options.fromPage
      });
      console.log("options.fromPage", this.data.fromPage);
    }

  },

  onShow() {
    // this.getAddress()
    // this.listPrivateProduct()
    this.handleFirst()
  },

  async handleFirst() {
    const id = 1;
    try {
      const res = await request({
        url: indexApi.productLeftApi(id),
        data: {}
      });
      this.setData({
        serviceTypeList: res.data,
        serviceTypeId: res.data[0].serviceTypeId
      })
      console.log("左侧导航", res.data);
      this.handleTwo()
    } catch (error) {
      console.error("Error:", error);
    }
  },

  async handleTwo(event) {
    if (event) {
      const id = event.currentTarget.dataset.dataid;
      this.setData({
        serviceTypeId: id,
      });
    } else {
      if (this.data.lastPage) {
        return;
      }
    }
    const id = this.data.serviceTypeId;
    const reqTemplate = {
      id: id,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    };
    try {
      const res = await request({
        url: indexApi.getPrivateProductByClassIdApi(),
        method: "POST",
        data: reqTemplate
      });
      if (res.data.length == 0) {
        this.setData({
          isEmpty: true
        })
      } else {
        this.setData({
          isEmpty: false
        })
      }
      this.setData({
        productList: res.data
      })
      console.log("商品productList", res.data);

    } catch (error) {
      console.error("Error:", error);
    }
  },

  //获取模板列表
  // async listPrivateProduct() {
  //   const pageNum = 1;
  //   const pageSize = 1000;
  //   try {
  //     const res = await request({
  //       url: indexApi.listPrivateProductApi(pageNum, pageSize),
  //       data: {}
  //     });

  //     if (res.data.records.length === 0) {
  //       console.log("为空");
  //       this.setData({
  //         isEmpty: false
  //       });
  //     } else {
  //       this.setData({
  //         productList:res.data.records,
  //         isEmpty: true
  //       });
  //     }

  //     console.log("获取模板列表", this.data.productList);
  //   } catch (error) {
  //     console.error("Error:", error);
  //   }
  // },


  //选择地址
  onChange(event) {
    const selectAddress = event.currentTarget.dataset.address
    this.setData({
      radio: event.detail,
      selectAddress: selectAddress
    });
    console.log("selectAddress", this.data.selectAddress);
  },


  //删除商品
  async deletePrivateProduct(Id) {
    const productId = Id
    try {
      const res = await request({
        url: indexApi.deletePrivateProductApi(),
        method: "POST",
        data: {
          productId
        }
      });
      if (res.code == 200) {
        wx.showToast({
          title: "删除成功",
          icon: "none",
        });
      }
      console.log(res);
      this.handleTwo()

    } catch (error) {
      console.error("Error:", error);
    }
  },

  //点击删除按钮
  async onClose(event) {
    const {
      position,
      instance
    } = event.detail;

    const productId = event.currentTarget.dataset.productid;
    console.log("event.currentTarget.dataset.addressid", productId);
    if (position === 'right') {
      wx.showModal({
        title: '删除确认',
        content: '确认删除该模板吗？',
        success: (res) => {
          if (res.confirm) {
            console.log(" 确保在删", productId);
            this.deletePrivateProduct(productId).then(() => {
              instance.close();
            }).catch(() => {
              instance.close();
            });
          } else {
            instance.close();
          }
        }
      });
    }
  },

  //修改商品
  templateChange(event) {
    console.log("event.currentTarget.dataset.productitem", event);
    const productItem = event.currentTarget.dataset.productitem;
    wx.navigateTo({
      url: `/packageUserCenter/pages/addMyProducts/index?productItem=${encodeURIComponent(JSON.stringify(productItem))}`,
    })
  },

  //确认
  beSure() {
    // if (this.data.fromPage == 1) {
    //   if (Object.keys(this.data.selectAddress).length === 0) {
    //     wx.showToast({
    //       title: "请选择一个地址",
    //       icon: "none",
    //     });
    //     return;
    //   }
    //   app.globalData.selectedAddress = this.data.selectAddress;
    //   console.log("app.globalData.selectedAddress", app.globalData.selectedAddress);
    //   wx.navigateBack()

    // } else {
    wx.navigateBack()
    // }
  },

  //添加商品
  toAddMyProducts() {
    wx.navigateTo({
      url: '/packageUserCenter/pages/addMyProducts/index?fromPage=2',
    })
  },
  toBack() {
    wx.navigateBack({
      delta: 1
    });
  }
})